{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import sys\n",
    "import os\n",
    "os.chdir('E:\\GitHub\\QA-abstract-and-reasoning')\n",
    "sys.path.append('E:\\GitHub\\QA-abstract-and-reasoning')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 Physical GPUs, 1 Logical GPUs\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from pgn.model import PGN\n",
    "from pgn.batcher import batcher\n",
    "from utils.saveLoader import Vocab\n",
    "from utils.config import VOCAB_PAD\n",
    "from utils.config_gpu import config_gpu\n",
    "config_gpu()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run utils/params.py\n",
    "# 产生输入数据\n",
    "vocab = Vocab(VOCAB_PAD)\n",
    "dataset = batcher(vocab, params)\n",
    "ds =iter(batcher(vocab, params))\n",
    "enc_data, dec_data = next(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = PGN(params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "64\n",
      "64\n",
      "64\n",
      "64\n",
      "64\n",
      "64\n",
      "64\n",
      "64\n",
      "64\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "for (batch, (enc_data, dec_data)) in enumerate(dataset.take(10)):\n",
    "    target = dec_data[\"dec_target\"]\n",
    "    enc_inp = enc_data[\"enc_input\"]\n",
    "    dec_inp = dec_data[\"dec_input\"]\n",
    "\n",
    "    enc_extended_inp = enc_data[\"extended_enc_input\"]\n",
    "    batch_oov_len = enc_data[\"max_oov_len\"]\n",
    "\n",
    "    enc_mask = enc_data[\"enc_mask\"]\n",
    "    dec_mask = dec_data[\"dec_mask\"]\n",
    "    print(len(target))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [],
   "source": [
    "final_dist,attentions, coverages = model(enc_inp, dec_inp, enc_extended_inp, \n",
    "                       batch_oov_len, enc_pad_mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=286, shape=(), dtype=int32, numpy=1>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.constant(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['enc_input', 'extended_enc_input', 'article_oovs', 'enc_len', 'article', 'enc_mask'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "enc_data.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=1188, shape=(16, 4), dtype=string, numpy=\n",
       "array([[b'\\xe6\\xb2\\xb3\\xe6\\xba\\x90', b'', b'', b''],\n",
       "       [b'', b'', b'', b''],\n",
       "       [b'\\xe6\\x9c\\x89\\xe6\\x95\\x88\\xe8\\xaf\\x81\\xe4\\xbb\\xb6', b'', b'',\n",
       "        b''],\n",
       "       [b'\\xe8\\xa4\\x90', b'\\xe9\\x82\\xa3\\xe7\\x99\\xbd',\n",
       "        b'\\xe4\\xbb\\x98\\xe8\\xb4\\x9f', b'\\xe7\\xbb\\xbf\\xe8\\x93\\x9d'],\n",
       "       [b'', b'', b'', b''],\n",
       "       [b'', b'', b'', b''],\n",
       "       [b'\\xe6\\xb3\\x95\\xe5\\x86\\x85', b'', b'', b''],\n",
       "       [b'\\xe5\\x85\\x88\\xe6\\x8b\\x93', b'', b'', b''],\n",
       "       [b'\\xe7\\xae\\xb1\\xe6\\xb2\\xb9\\xe8\\xa6\\x81', b'', b'', b''],\n",
       "       [b'\\xe8\\x8e\\xab\\xe5\\xa4\\xa7', b'\\xe9\\xbe\\x99\\xe8\\x83\\x8e', b'',\n",
       "        b''],\n",
       "       [b'\\xe7\\x9c\\x8b\\xe7\\x85\\xa7', b'', b'', b''],\n",
       "       [b'\\xe8\\xb0\\x83\\xe8\\xa1\\xa8\\xe5\\x99\\xa8',\n",
       "        b'\\xe5\\x93\\x88\\xe5\\xb0\\x94\\xe6\\xbb\\xa8\\xe5\\xb8\\x82',\n",
       "        b'\\xe5\\xbb\\xb6\\xe5\\xa0\\xa1', b''],\n",
       "       [b'\\xe6\\x97\\xa0\\xe5\\x89\\xaf\\xe4\\xbd\\x9c\\xe7\\x94\\xa8', b'', b'',\n",
       "        b''],\n",
       "       [b'\\xe7\\x8b\\xbc\\xe5\\xa4\\xb4', b'', b'', b''],\n",
       "       [b'', b'', b'', b''],\n",
       "       [b'\\xe7\\xa7\\x80\\xe5\\x84\\xbf', b'', b'', b'']], dtype=object)>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "enc_data[\"article_oovs\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=1200, shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.zeros(1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf2.0]",
   "language": "python",
   "name": "conda-env-tf2.0-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
